package com.gzk.oeno.ihm.opinion;

import java.util.Arrays;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;

import com.gzk.oeno.tech.ui.CompoundIcon;
import com.gzk.oeno.tech.ui.CompoundIcon.Axis;
import com.gzk.oeno.tech.ui.Formator;
import com.gzk.utils.ihm.UIUtils;

public class OpinionUIUtils {

	private static final Icon RATE_ICON = UIUtils.loadSilentIcon("icons/star_16.png");
	private static final Icon RATE_HALF_ICON = UIUtils.loadSilentIcon("icons/star_half_16.png");
	private static final Icon RATE_1Q_ICON = UIUtils.loadSilentIcon("icons/star_1q_16.png");
	private static final Icon RATE_3Q_ICON = UIUtils.loadSilentIcon("icons/star_3q_16.png");
	
	private OpinionUIUtils(){}
	
	public static JComponent buildLongRateLabel(float value, int maxStars, int maxValue){
		float divisor = 1f;
		if (maxValue > maxStars)
			divisor = maxValue / (float) maxStars;
		
		Icon fullIcon = null;
		
		int x = Math.round(value / divisor * 4);
		Icon last = null;
		switch (x % 4){
		case 0:
			last = RATE_ICON;
			break;
		case 1:
			last = RATE_1Q_ICON;
			break;
		case 2:
			last = RATE_HALF_ICON;
			break;
		case 3:
			last = RATE_3Q_ICON;
			break;
		}
		int nbstars = x / 4;
		if (x % 4 == 0)
			nbstars--;
		if (nbstars > 0){
			fullIcon = new CompoundIcon(Axis.X_AXIS, new CompoundIcon(Axis.X_AXIS, getXIcons(nbstars)), last);
		} else {
			fullIcon = last;
		}
		JLabel label = new JLabel(fullIcon);
		label.setToolTipText(Formator.getFormDecimalFormat().format(value) + "/" + maxValue);
		return label;
	}
	
	
	public static JComponent buildShortRateLabel(int nb, int maxStars, int maxValue){
		int divisor = 1;
		if (maxValue > maxStars)
			divisor = maxValue / maxStars;
		int round = nb / divisor;
		
		Icon enable = null;
		switch(round){
		case 3:
			enable = new CompoundIcon(Axis.Y_AXIS, 
							new CompoundIcon(Axis.X_AXIS, getXIcons(2)), 
							RATE_ICON);
			break;
		case 4:
			enable = new CompoundIcon(Axis.Y_AXIS, 
							new CompoundIcon(Axis.X_AXIS, getXIcons(2)), 
							new CompoundIcon(Axis.X_AXIS, getXIcons(2)));
			break;
		case 5:
			enable = new CompoundIcon(Axis.Y_AXIS, 
							new CompoundIcon(Axis.X_AXIS, getXIcons(3)), 
							new CompoundIcon(Axis.X_AXIS, getXIcons(2)));
			break;
		default:
			enable = new CompoundIcon(Axis.X_AXIS, getXIcons(round));
		}
		return new JLabel(enable);
	}
	
	private static Icon[] getXIcons(int nb){
		Icon[] params = new Icon[nb];
		Arrays.fill(params, RATE_ICON);
		return params;
	}
}
